// patrolnpc.txt
// Creature attacks anything it hates nearby, and spends the rest of its time patrolling a path.
// Once it has won, it returns to its home.
// Memory Cells:
//   Cell 0 - Number of path to follow.
//	 Cell 1 - If 0, just loops around path. If 1, walks to end of path, waits a little, and returns home.
//      If 2, goes to end of path and just stops.
//   Cell 2 - Node if talked to. 0 means no conversation
//   Cell 3,4 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.

begincreaturescript;

variables;

short i,target;
short got_bonus = 0;

body;

beginstate INIT_STATE;
	set_summon_level(ME,1);
	set_level(ME,28);
	set_attack_bonus(ME,10);
	break;

beginstate DEAD_STATE;
	if ((get_memory_cell(3) != 0) || (get_memory_cell(4) != 0))
		inc_flag(get_memory_cell(3),get_memory_cell(4),1);
break;

beginstate START_STATE;
	
	if ((got_bonus == 0) && (get_stat(20) > 11) && (dist_to_pc() <= 6)) {
		got_bonus = 1;
		print_named_str(ME,"is strengthened by your presence.");
		set_boss_level(ME,1);
		set_new_abil(ME,20);
		if (gf(58,17) == 0) {
			sf(58,17,1);
			begin_talk_mode(4);
			}
		}
		
	if (get_foe_target(ME,6,0)) {
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	if (get_memory_cell(1) == 0) 
		follow_path(ME,get_memory_cell(0),1);
		else {
			if ((my_dist_from_start() >= 2) && (get_memory_cell(1) != 2)) {
				if (get_ran(1,1,100) < 40) 
					return_to_start(ME,1);
				}
			if ((get_ran(1,1,100) < 15) || (get_memory_cell(1) == 2)) {
				follow_path(ME,get_memory_cell(0),0);
				set_state(4);
				}
			}

	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);

	if ((got_bonus == 0) && (get_stat(20) > 11) && (dist_to_pc() <= 6)) {
		got_bonus = 1;
		print_named_str(ME,"is strengthened by your presence.");
		set_boss_level(ME,1);
		set_attack_bonus(ME,8);
		if (gf(58,17) == 0) {
			sf(58,17,1);
			begin_talk_mode(4);
			}
		}

	do_attack();
break;

beginstate 4; // patrol
	if (get_foe_target(ME,8,0)) {
		do_attack();
		set_state(3);
		}
	if (who_shot_me() >= 0) {
		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}
	if (follow_path(ME,get_memory_cell(0),0) == TRUE)
		set_state(START_STATE);
break;

beginstate TALKING_STATE;
		print_str("Talking: It is too busy assaulting Gazaki-Uss to be able to");
		print_str("  speak with you.");

break;